/**
 * ztree插件的封装
 */
(function () {

    var $Select2 = function (id, url) {
        this.id = id;
        this.url = url;
        this.paginate = true;//分页加载
        this.limit = 10;//分页每次请求数量
        this.condition = null;//查询关键词
        this.delay = 500;//用户在停止键入之后等待多久发送请求
    };

    $Select2.prototype = {

        /**
         * 设置请求等待时间
         * @param delay
         */
        setDelay: function (delay) {
            this.delay = delay;
        },
        /**
         * 设置每次请求的数据量
         * @param limit
         */
        setLimit: function (limit) {
            this.limit
        },

        /**
         * 初始化ztree的设置
         */
        initSetting: function () {
            var settings = {
                view: {
                    dblClickExpand: true,
                    selectedMulti: false
                },
                data: {simpleData: {enable: true}},
                callback: {
                    onClick: this.onClick,
                    onDblClick: this.ondblclick
                }
            };
            return settings;
        },

        /**
         * 初始化select
         */
        init: function () {
            $("#" + this.id).select2({
                ajax: {
                    url: this.url,
                    data: function (params) {
                        return {
                            term: params.term,
                            type: 'public',
                            limit: this.limit,
                            offset: params.page != null ? ((params.page - 1) * limit) : 0
                        }
                    }
                }
            });
        }
    }

    window.$Select2 = $Select2;

}());